home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 06 / worddll / novanew.c < prev    next >
Text File  |  1990-11-01  |  10KB  |  288 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  PROGRAM     : NovaNew.c                                                *
  4.  *                                                                         *
  5.  *  PURPOSE     : Extends Word for Windows "New" function by providing     *
  6.  *                two dialogs to facilitate the selection of a template    *
  7.  *                                                                         *
  8.  *  FUNCTIONS   : LibMain()           - Generalized library initialization *
  9.  *                                      function.  Used to retrieve        *
  10.  *                                      handle.                            *
  11.  *                                                                         *
  12.  *                WEP ()              - Generalized library termination    *
  13.  *                                      function.  Returns true.           *
  14.  *                                                                         *
  15.  *                NovaNew()           - Entry point for calls from WinWord *
  16.  *                                                                         *
  17.  *                fnNew()             - Dialog function                    *
  18.  *                                                                         *
  19.  *                ExtractTemplate()   - Extracts the last word and         *
  20.  *                                      validates that it is the name      *
  21.  *                                      of a template file                 *
  22.  *                                                                         *
  23.  *                LastWord()          - Extract the last word from a       *
  24.  *                                      listbox entry.                     *
  25.  *                                                                         *
  26.  *                FillListBox()       - Reads the contents of a file into  *
  27.  *                                      a listbox.                         *
  28.  *                                                                         *
  29.  ***************************************************************************/
  30.  
  31. #include "windows.h"
  32. #include "novanew.h"
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <ctype.h>
  37.  
  38.  
  39. int FAR PASCAL LibMain(HANDLE hModule,WORD wDataSeg,WORD cbHeapSize,LPSTR lpszCmdLine);
  40. int FAR PASCAL WEP (BOOL bSystemExit);
  41. int NovaNew(LPSTR lpTemplate);
  42. int far PASCAL fnNew(HWND hwnd,unsigned msg,WORD wParam,LONG lParam);
  43. BOOL ExtractTemplate(void);
  44. int LastWord(PSTR szString);
  45. BOOL FillListBox(HWND hwnd, int nControl,PSTR szFileName);
  46.  
  47. #define CCHLBMAX 3000        /* Maximum size of listbox data */
  48. #define CCHBUFFERMAX 200   /* Maximum size of text in the template listbox */
  49. char     rgchBuffer[CCHBUFFERMAX+1]; /* General purpose buffer */
  50. char     rgchTemplate[CCHBUFFERMAX+1];
  51. HANDLE hInstance;            /* Library instance handle */
  52.  
  53. /****************************************************************************
  54.    FUNCTION: LibMain(HANDLE, WORD, WORD, LPSTR)
  55.  
  56.    PURPOSE:  Is called by LibEntry.  LibEntry is called by Windows when
  57.              the DLL is loaded.  The LibEntry routine is provided in
  58.              the LIBENTRY.OBJ in the SDK Link Libraries disk.  (The
  59.              source LIBENTRY.ASM is also provided.)
  60.  
  61.              LibEntry initializes the DLL's heap, if a HEAPSIZE value is
  62.              specified in the DLL's DEF file.  Then LibEntry calls
  63.              LibMain.  The LibMain function below satisfies that call.
  64.  
  65.              The LibMain function should perform additional initialization
  66.              tasks required by the DLL.  In this example, no initialization
  67.              tasks are required.  LibMain should return a value of 1 if
  68.              the initialization is successful.
  69.  
  70. *******************************************************************************/
  71. int FAR PASCAL LibMain(HANDLE hModule,WORD wDataSeg,WORD cbHeapSize,LPSTR lpszCmdLine)
  72. {
  73.     hInstance = hModule;
  74.     return 1;
  75. }
  76.  
  77.  
  78. /****************************************************************************
  79.     FUNCTION:  WEP(int)
  80.  
  81.     PURPOSE:  Performs cleanup tasks when the DLL is unloaded.  WEP() is
  82.               called automatically by Windows when the DLL is unloaded (no
  83.               remaining tasks still have the DLL loaded).  It is strongly
  84.               recommended that a DLL have a WEP() function, even if it does
  85.               nothing but returns success (1), as in this example.
  86.  
  87. *******************************************************************************/
  88. int FAR PASCAL WEP (BOOL bSystemExit)
  89. {
  90.     return(TRUE);
  91. }
  92.  
  93. int NovaNew(LPSTR lpTemplate)
  94. {
  95.     int ret;
  96.  
  97.     rgchTemplate[0] = 0;
  98.     lpTemplate[0] = 0;
  99.     ret = DialogBox(hInstance, "GET_TEMPLATE", GetActiveWindow(),
  100.                     (FARPROC)fnNew);
  101.     lstrcpy (lpTemplate, rgchTemplate);
  102.     return ret;
  103. }
  104.  
  105. /*--------------------------------------------------------------------*/
  106. /* ** Dialog function for NovaNew */
  107. int far PASCAL fnNew(HWND hwnd,unsigned msg,WORD wParam,LONG lParam)
  108. {
  109.    LONG lSel;
  110.    int nLen;
  111.  
  112.    switch (msg)
  113.    {
  114.       case WM_INITDIALOG:
  115.          FillListBox(hwnd, GET_TEMPLATE_GRP_LB, "TEMPLATE.LB");
  116.          rgchTemplate[0] = 0;
  117.          break;
  118.  
  119.       case WM_COMMAND:
  120.          switch (wParam)
  121.          {
  122.             case IDOK:
  123.                if (ExtractTemplate())
  124.                   EndDialog(hwnd, wParam);
  125.                return TRUE;
  126.  
  127.             case IDCANCEL:
  128.                EndDialog(hwnd, wParam);
  129.                return TRUE;
  130.  
  131.             case GET_TEMPLATE_LB:
  132.                if (HIWORD(lParam) == LBN_DBLCLK ||
  133.                    HIWORD(lParam) == LBN_SELCHANGE)
  134.                {
  135.                   lSel = SendDlgItemMessage(hwnd, wParam,
  136.                                    LB_GETCURSEL,0,0L);
  137.                   nLen = (int)SendDlgItemMessage(hwnd, wParam,
  138.                                    LB_GETTEXTLEN, LOWORD(lSel), 0L);
  139.                   rgchTemplate[0] = 0;
  140.                   if (nLen < sizeof(rgchTemplate))
  141.                       SendDlgItemMessage(hwnd, wParam, LB_GETTEXT,
  142.                                    LOWORD(lSel),
  143.                                    (LONG)(LPSTR)rgchTemplate);
  144.                   if ((HIWORD(lParam) == LBN_DBLCLK) &&
  145.                       (ExtractTemplate()))
  146.                      EndDialog(hwnd, IDOK);
  147.                }
  148.                return TRUE;
  149.  
  150.             case GET_TEMPLATE_GRP_LB:
  151.                if (HIWORD(lParam) == LBN_SELCHANGE)
  152.                {
  153.                   lSel = SendDlgItemMessage(hwnd, wParam,
  154.                                    LB_GETCURSEL,0,0L);
  155.                   nLen = (int)SendDlgItemMessage(hwnd, wParam,
  156.                                    LB_GETTEXTLEN, LOWORD(lSel), 0L);
  157.                   rgchTemplate[0] = 0;
  158.                   if (nLen < sizeof(rgchTemplate))
  159.                       SendDlgItemMessage(hwnd, wParam, LB_GETTEXT,
  160.                                    LOWORD(lSel),
  161.                                    (LONG)(LPSTR)rgchBuffer);
  162.                   LastWord(rgchBuffer);
  163.                   strcat(rgchBuffer, ".LB");
  164.                   /* if a new model name */
  165.                   if (strcmp(rgchTemplate, rgchBuffer) != 0)
  166.                   {
  167.                      strcpy(rgchTemplate, rgchBuffer);
  168.                      FillListBox(hwnd, GET_TEMPLATE_LB, rgchTemplate);
  169.                   }
  170.                   return TRUE;
  171.                }
  172.                else
  173.                   return FALSE;
  174.                break;
  175.  
  176.             default:
  177.                return FALSE;
  178.          }
  179.          break;
  180.  
  181.       default:
  182.          return FALSE;
  183.    }
  184.    return TRUE;
  185. }
  186.  
  187. BOOL ExtractTemplate(void)
  188. {
  189.    OFSTRUCT rgbOpen;
  190.    int      fp;
  191.  
  192.    LastWord(rgchTemplate);
  193.    strcat(rgchTemplate, ".DOT");
  194.  
  195.    fp = OpenFile(rgchTemplate, (LPOFSTRUCT)&rgbOpen, OF_EXIST);
  196.    if (fp == -1)
  197.       return FALSE;
  198.    return TRUE;
  199. }
  200.  
  201. /* Extract the word following at least two blanks */
  202. int LastWord(PSTR szString)
  203. {
  204.    PSTR p, p2;
  205.  
  206.    for (p = szString; *p && !(*p == ' ' && *(p+1) == ' '); p++)
  207.       ;
  208.    while (*p == ' ')
  209.       p++;
  210.    for (p2 = p; isalnum(*p2); p2++)
  211.       ;
  212.    *p2 = 0;
  213.    memcpy(szString, p, strlen(p) + 1);
  214.    return strlen(szString);
  215. }
  216.  
  217. BOOL FillListBox(HWND hwnd, int nControl,PSTR szFileName)
  218. {
  219.    int      fp;
  220.    long     lcch;
  221.    LPSTR    lpch;
  222.    LPSTR    lpchItem;
  223.    HANDLE   hLBData;
  224.    WORD     cch;
  225.    HWND     hCtl;
  226.    char     ch;
  227.    OFSTRUCT rgbOpen;
  228.  
  229.    hCtl=GetDlgItem(hwnd,nControl);
  230.    SendMessage(hCtl, LB_RESETCONTENT, 0, 0L);
  231.    InvalidateRect(hCtl, NULL, TRUE);
  232.    fp=_lopen(szFileName, READ);
  233.    if (fp == -1)
  234.    {
  235.       fp = OpenFile(szFileName, (LPOFSTRUCT)&rgbOpen ,OF_READ);
  236.       if (fp == -1)
  237.          return FALSE;
  238.    }
  239.    /* Get size of file, then reset file pointer */
  240.    lcch = _llseek(fp, 0L, 2);
  241.    cch = LOWORD(lcch);
  242.    _llseek(fp, 0L, 0);
  243.  
  244.    hLBData = GlobalAlloc(GMEM_MOVEABLE, (LONG)cch+1);
  245.    /* If not enough memory, quit */
  246.    if (!hLBData)
  247.    {
  248.       _lclose(fp);
  249.       return FALSE;
  250.    }
  251.    /* Read file into buffer */
  252.    if (!(lpch = GlobalLock(hLBData)))
  253.    {
  254.       _lclose(fp);
  255.       GlobalFree(hLBData);
  256.       return FALSE;
  257.    }
  258.    _lread(fp, lpch, cch);
  259.    _lclose(fp);
  260.       if (cch && *(lpch+cch-1)==0x1a)
  261.          cch--; /* get rid of eof */
  262.    *(lpch + cch) = 0;      /* zero terminate the thing */
  263.    SendMessage(hCtl, WM_SETREDRAW, (WORD)FALSE, 0L);
  264.    for (lpchItem = lpch; *lpchItem; lpch++)
  265.    {
  266.       if (*lpch==0x0a || *lpch==0x0d || *lpch==0)
  267.       {
  268.          ch=*lpch;
  269.          if ((lpch-lpchItem) < CCHBUFFERMAX)
  270.             *lpch=0;
  271.          else
  272.             *(lpchItem+CCHBUFFERMAX)=0; /* Max length */
  273.          SendMessage(hCtl, LB_ADDSTRING, (WORD)0, (LONG)lpchItem);
  274.          *lpch=ch;
  275.          if (*lpch)
  276.             while (*lpch==0x0a || *lpch==0x0d)
  277.                lpch++;
  278.          lpchItem=lpch;
  279.       }
  280.    }
  281.    SendMessage(hCtl, WM_SETREDRAW, (WORD)TRUE, 0L);
  282.    InvalidateRect(hCtl,(LPRECT)NULL,TRUE);
  283.    GlobalUnlock(hLBData);
  284.    GlobalFree(hLBData);
  285.    return TRUE;
  286. }
  287.  
  288.